home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / LZW-TEST.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  5KB  |  164 lines

  1. /*--- LZW-TEST.C ------------------------- Listing 5 ----
  2. * Contents:  This is the driver for the
  3. *            the LZW data compression program.  It
  4. *            contains the following routines:
  5. *               main
  6. *               print_string_table
  7. *
  8. * Author:    Dwayne Phillips
  9. * Compiler: Microsoft C 6.0a, BC++ 2.0
  10. *           Note: Must compile with at least an 8K stack
  11. * Switches: DEBUG - if defined debugging data is shown
  12. * Date:      February 1991
  13. * May be used freely is authorship is acknowledged
  14. *-------------------------------------------------------*/
  15. #include "lzw.h"
  16.  
  17. #if defined(__TURBOC__)
  18. #include <dos.h> /* for stack size control */
  19. #endif
  20.  
  21. /*------------------------------------------------------
  22.  * Debug routine
  23.  *----------------------------------------------------*/
  24. void print_string_table( table_item *string_table,
  25.                          char *pre, int lo, int hi )
  26. {
  27.     int s;
  28.  
  29.     for ( s = lo; s <= hi; s++ ) {
  30.         printf ( "\n %s> table[%d] = ", pre, s );
  31.         if ( string_table[s].num <= BASE_TABLE &&
  32.             isprint ( string_table[s].num ))
  33.             printf( "'%c' ", string_table[s].num );
  34.         else
  35.             printf ( "%3d ", string_table[s].num );
  36.         printf ( isprint(string_table[s].character ) ?
  37.                "'%c' " : "%2dd ", string_table[s].character );
  38.     }
  39. } /* ends print_string_table */
  40.  
  41. /*-------------------------------------------------------
  42.  * main()
  43.  *-----------------------------------------------------*/
  44. table_item string_table[TABLE_SIZE];
  45.  
  46. void main ( int argc, char **argv )
  47. {
  48.     char  input_file_name[80],
  49.           output_file_name[80],
  50.           response[80];
  51.  
  52.     FILE *infile, *outfile;
  53.  
  54.     #if defined(__TURBOC__)
  55.     _stklen = 0x4000;
  56.     #endif
  57.  
  58.     if ( argc >= 2 )
  59.         strcpy( response, argv[1] );
  60.     else
  61.         response[0] = 0;
  62.  
  63.     if ( argc >= 3 )
  64.         strcpy ( input_file_name, argv[2] );
  65.     else
  66.         input_file_name[0] = 0;
  67.  
  68.     if ( argc >=4 )
  69.         strcpy ( output_file_name, argv[3] );
  70.     else
  71.         output_file_name[0] = 0;
  72.  
  73.     if ( strlen ( response ) == 0 ) {
  74.          printf ( "\n\nData compression using LZW Coding"
  75.                   "\n        Do you want to:"
  76.                   "\n           c) compress a file"
  77.                   "\n           d) decompress a file"
  78.                   "\n           q) quit\n"
  79.                   "\n              (c, d, q) ___\b\b" );
  80.          gets ( response );
  81.     }
  82.  
  83.     if ( tolower ( response[0]) == 'q') {
  84.          printf( "\ngoodbye" );
  85.          exit ( 1 );
  86.     }
  87.  
  88.     /*------------------------
  89.      *   Decompression Case
  90.      *----------------------*/
  91.     if ( tolower ( response[0] ) == 'd' ) {
  92.         if ( strlen ( input_file_name ) == 0 ) {
  93.             printf("\n\nName of the file to decompress:\n\t--");
  94.             gets ( input_file_name );
  95.         }
  96.         if ( strlen ( output_file_name ) == 0 ) {
  97.             printf ( "\n\nName of the output file:\n\t--" );
  98.             gets ( output_file_name );
  99.         }
  100.  
  101.         /* open file for read only */
  102.         if (( infile = fopen (input_file_name, "r+b" )) == NULL )
  103.         {
  104.             printf ( "Can't open %s.\n", input_file_name );
  105.             exit ( 1 );
  106.         }
  107.         fseek ( infile, 0L, SEEK_END );
  108.         if ( ftell ( infile ) < 2 ){
  109.              printf ( "\nERROR - input file is empty" );
  110.              exit ( 0 );
  111.         }
  112.         (void) fseek ( infile, 0L, SEEK_SET );
  113.  
  114.         /* create the output file */
  115.         if (( outfile = fopen (output_file_name, "w+b" ))
  116.                  == NULL) {
  117.             printf ( "Can't open %s.\n", output_file_name );
  118.             exit ( 1 );
  119.         }
  120.  
  121.         decompression_routine ( string_table, infile, outfile );
  122.         fclose ( infile );
  123.         fclose ( outfile );
  124.     }
  125.  
  126.     /*----------------------
  127.          Compression Case
  128.     -----------------------*/
  129.     if( tolower ( response[0]) == 'c') {
  130.         if ( strlen ( input_file_name ) == 0) {
  131.             printf ( 
  132.               "\n\nName of the file to be compressed:\n\t--" );
  133.             gets ( input_file_name );
  134.         }
  135.         if ( strlen ( output_file_name ) == 0 ) {
  136.             printf( "\n\nName of the output file:\n\t--" );
  137.             gets ( output_file_name );
  138.         }
  139.  
  140.         /* open file for read only */
  141.         if (( infile = fopen ( input_file_name, "r+b")) 
  142.              == NULL) {
  143.               printf ( "Can't open %s.\n", input_file_name );
  144.               exit ( 1 );
  145.         }
  146.         fseek ( infile, 0L, SEEK_END );
  147.         if ( ftell ( infile ) < 2 ) {
  148.              printf ( "\nERROR - input file is empty" );
  149.              exit ( 0 );
  150.         }
  151.         (void) fseek ( infile, 0L, SEEK_SET );
  152.  
  153.         /* create the output file */
  154.         if (( outfile = fopen ( output_file_name, "w+b" )) 
  155.             == NULL ) {
  156.              printf ( "Can't open %s.\n", output_file_name );
  157.              exit ( 1 );
  158.         }
  159.  
  160.         compression_routine ( string_table, infile, outfile );
  161.         fclose ( infile );
  162.         fclose ( outfile );
  163.     } /* ends else compress input file to output file */
  164. } /* ends main  */